package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.ExportTask;
import com.example.domain.User;
import com.example.constract.UserStatus;
import com.example.constract.UserStatus5;
import com.example.mapper.ExportTaskMapper;
import com.example.service.ExportTaskServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class JsonHandlerController {

    @Autowired
    private ExportTaskMapper exportTaskMapper;

    @Autowired
    private ExportTaskServiceImpl exportTaskService;
    /***
     * 对应day05,能够让枚举值有用，但是有问题
     */
    @RequestMapping(value = "/test/status5" )
    List<ExportTask> status5(@RequestBody User user)  {
            log.info("执行方法");
        LambdaQueryWrapper<ExportTask> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        UserStatus disable = UserStatus.DISABLE;
        LambdaQueryWrapper<ExportTask> eq = objectLambdaQueryWrapper.eq(ExportTask::getStatus, disable);
        List<ExportTask> exportTasks = exportTaskMapper.selectList(eq);
        log.info("执行方法byId:{}", JSON.toJSONString(exportTasks));
        return exportTasks;
    }

    @RequestMapping(value = "/test/insertHandler" )
    boolean insertHandler()  {
        log.info("执行方法");
        ExportTask exportTask=new ExportTask();
        UserStatus disable = UserStatus.DISABLE;
        exportTask.setTaskName("11");
        exportTask.setStatus(disable);
        boolean save = exportTaskService.save(exportTask);
        log.info("执行方法exportTask:{}", JSON.toJSONString(exportTask));
        return save;
    }

    /**
     * 对应day06 使用自定义typeHandler，自定义枚举类处理规则
     * 存在的不足，和前文提到的Conventer，Deserialzer，Serializer一样，我们不可能为每一个枚举，都写一套完善的转换规则。
     * 所以就有了通用化的需求。
     * @param user
     * @return
     */

    @RequestMapping(value = "/test/status6" )
    List<ExportTask> status6(@RequestBody User user)  {
        log.info("执行方法");
        LambdaQueryWrapper<ExportTask> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        UserStatus disable = UserStatus.DISABLE;
        LambdaQueryWrapper<ExportTask> eq = objectLambdaQueryWrapper.eq(ExportTask::getStatus, disable);
        List<ExportTask> exportTasks = exportTaskMapper.selectList(eq);
        log.info("执行方法byId:{}", JSON.toJSONString(exportTasks));
        return exportTasks;
    }

    /**
     * 对应day07  通用TypeHandler-可正常使用 这还不够简化，因为每添加一个枚举常量，我们就要去register一个TypeHandler，显然不够通用化
     * @param user
     * @return
     */
    @RequestMapping(value = "/test/status7" )
    List<ExportTask> status7(@RequestBody User user)  {
        log.info("执行方法");
        LambdaQueryWrapper<ExportTask> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        UserStatus5 disable = UserStatus5.DISABLE;
        LambdaQueryWrapper<ExportTask> eq = objectLambdaQueryWrapper.eq(ExportTask::getStatus5, disable);
        List<ExportTask> exportTasks = exportTaskMapper.selectList(eq);
        log.info("执行方法byId:{}", JSON.toJSONString(exportTasks));
        return exportTasks;
    }


    /**
     * 对应day08  注册TypeHandler最终版
     * 可以在springboot中加入配置项，配置枚举常量的包名
     * 再在注册时，获取包下所有的class进行反射，解放双手
     * @param user
     * @return
     */
    @RequestMapping(value = "/test/status8" )
    List<ExportTask> status8(@RequestBody User user)  {
        log.info("执行方法");
        LambdaQueryWrapper<ExportTask> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        UserStatus5 disable = UserStatus5.DISABLE;
        LambdaQueryWrapper<ExportTask> eq = objectLambdaQueryWrapper.eq(ExportTask::getStatus5, disable);
        List<ExportTask> exportTasks = exportTaskMapper.selectList(eq);
        log.info("执行方法byId:{}", JSON.toJSONString(exportTasks));
        return exportTasks;
    }


}
